package com.umfwechat.h5.controller.settlement;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.ReqMessage;
import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.constant.ConfigConsts;
import com.umfwechat.common.constant.MsgConsts;
import com.umfwechat.common.constant.ValueConsts;
import com.umfwechat.common.prop.UmfPropModel;
import com.umfwechat.util.HttpUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.ResultJsonUtil;
import com.umfwechat.util.StringUtil;
import com.umpay.sp.util.AmountUtil;
/**
 * 提前结算手续费、到账金额
 * @author css
 *
 */
@Controller
@RequestMapping("h5/serviceChargeController")
public class ServiceChargeController {

	private static Logger logger = LoggerFactory.getLogger(ServiceChargeController.class);
	
	/**
	 * 提前结算申请的手续费、到账金额接口
	 * @param request
	 * @param response
	 * @param merId
	 * @param amount
	 * @return
	 */
 	@RequestMapping(value="/queryPoundage",method = {RequestMethod.POST,RequestMethod.GET },produces="text/html;charset=UTF-8")
 	@ResponseBody
 	public String queryPoundage(HttpServletRequest request ,HttpServletResponse response ,String merId ,String amount){
 		logger.info("【查询商户提前结算的手续费接口】开始");
 		try {
			if(StringUtil.isEmpty(amount)){
				logger.info("获取申请提前结算金额为空！");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			if(StringUtil.isEmpty(merId)){
				logger.info("获取商户号为空！");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433077);
			}
			//元转分
			BigDecimal bignum = new BigDecimal(amount);
			BigDecimal num = new BigDecimal(100);
			amount = bignum.multiply(num).setScale(0).toString();
			logger.info("金额为" +amount);
			//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~查询手续费接口~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			String url = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.BORROW_RATE_SERVICE_CHARGE);
			Map<String , Object> param = new HashMap<String, Object>();
			ReqMessage req = ReqMessageUtil.makeReq();
			param.put(CommonConsts.RPID, StringUtil.trim(req.getRpid()));
			param.put(CommonConsts.REQDATE, StringUtil.trim(req.getReqDate()));
			param.put(CommonConsts.REQTIME, StringUtil.trim(req.getReqTime()));
			param.put(CommonConsts.MERID, merId);
			param.put(CommonConsts.AMOUNT, amount);
			param.put(CommonConsts.PRODUCTID,ValueConsts.PRODUCTID_ES_PAYMENT_CODE);
			logger.info("【查询提前结算手续费接口】入参" + param);
			logger.info("请求地址" + url );
			Map<String, Object> resMap = HttpUtil.httpPostForm2Xml(url, param);
			logger.info("【查询提前结算手续费接口】出参" + resMap);
			if(!resMap.get(CommonConsts.RETCODE).equals(RetCode.SUCCESS)){
				logger.info("查询提前结算手续费接口失败！");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433016);
			}
			String feeamtNo = resMap.get(CommonConsts.FEEAMT).toString();
			String feeamt = AmountUtil.Cent2Dollar2(feeamtNo);
			logger.info("提前结算金额amount：" + amount +"手续费为feeamt:" + feeamt);
			//~~~~~~~~~~~~计算到账金额~~~~~~~~~~~~~
			BigDecimal bigFee = new BigDecimal(feeamtNo);
			String  amtNo = bignum.multiply(num).subtract(bigFee).toString();
			String amt = AmountUtil.Cent2Dollar2(amtNo);
			logger.info("到账金额amt：" + amt);
			Map<String, Object> resultMap = new HashMap<String ,Object>(); 
			resultMap.put(CommonConsts.FEEAMT, feeamt);
			resultMap.put(CommonConsts.AMT, amt);
			return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.I00433114 , resultMap);
		} catch (Exception e) {
			logger.info("查询提前结算手续费接口失败！");
		}
 		return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433016 );
 	}
}
